/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.excesys.exsecs.om.entity.OmInspectionArea;
import com.excesys.exsecs.om.mapper.OmInspectionAreaMapper;
import com.excesys.exsecs.om.service.OmInspectionAreaClassificationListService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;

/**
 * @date 2023-05-19
 */
@RequiredArgsConstructor
@Service
public class OmInspectionAreaClassificationListServiceImpl extends ServiceImpl<OmInspectionAreaMapper, OmInspectionArea>
		implements OmInspectionAreaClassificationListService {

	private final OmInspectionAreaMapper omInspectionAreaMapper;

	@Override
	public String getList(String exId) {
		List<OmInspectionArea> list = omInspectionAreaMapper.getList(exId);
		Format format = new DecimalFormat("000");
		if (list.size() <= 0)
			return "XJ001";
		if (list.size() <= 999 && list.size() > 0)
			return "XJ" + format.format(list.size() + 1);
		else
			return "";
	}

	@Override
	public List<Tree<String>> getTreeList(List<OmInspectionArea> list) {
		System.out.println("输出list：" + list);
		List<Tree<String>> lists = TreeUtil.build(list, "0", (node, tree) -> {
			tree.setId(node.getId().toString());
			tree.setParentId(node.getParentId().toString());
			// 额外的值
			tree.put("buildingsId", node.getBuildingsId());
			tree.put("floorCode", node.getFloorCode());
			tree.put("subareaCode", node.getSubareaCode());
			tree.put("inspectionClassification", node.getInspectionClassification());
			tree.put("inspectionClassificationName", node.getInspectionClassificationName());
			tree.put("inspectionCode", node.getInspectionCode());
			tree.put("inspectionInfo", node.getInspectionInfo());
			tree.put("inspectionSize", node.getInspectionSize());
			tree.put("associatedDevice", node.getAssociatedDevice());
			tree.put("inspectionName", node.getInspectionName());
			tree.put("associatedDeviceFlag", node.getAssociatedDeviceFlag());
		});
		System.out.println("转换后树状数据：" + JSON.toJSONString(lists));
		return lists;
	}

	@Override
	public List<OmInspectionArea> getAreaList(String code, String name) {
		List<OmInspectionArea> list = omInspectionAreaMapper.getAreaList(code, name);
		return list;
	}

	@Override
	public List<OmInspectionArea> getLists(String id) {
		return omInspectionAreaMapper.getLists(id);
	}

	@Override
	public Page setPage(List<OmInspectionArea> omInspectionAreas, Page page) {
		if (omInspectionAreas == null) {
			return null;
		}
		int size = omInspectionAreas.size();
		int pageSize = (int) page.getSize();
		if (pageSize > size) {
			pageSize = size;
		}
		int pageCurrent = (int) page.getCurrent();
		if (pageSize != 0) {
			// 求出最大页数，防止currentPage越界
			int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;

			if (pageCurrent > maxPage) {
				pageCurrent = maxPage;
			}
		}
		// 当前页第一条数据的下标
		int curIdx = pageCurrent > 1 ? (pageCurrent - 1) * pageSize : 0;

		List pageList = new ArrayList();

		// 将当前页的数据放进pageList
		for (int i = 0; i < pageSize && curIdx + i < size; i++) {
			pageList.add(omInspectionAreas.get(curIdx + i));
		}

		page.setCurrent(pageCurrent).setSize(pageSize).setTotal(omInspectionAreas.size()).setRecords(pageList);
		return page;
	}

}
